<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" lay-filter="reload">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="sign_out">
                            <option value="">是否退出</option>
                            <option value="1">否</option>
                            <option value="2">是</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="like">
                            <option value="">我的最爱</option>
                            <option value="1">否</option>
                            <option value="2">是</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="keys" placeholder="工号" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="import">
        <i class="layui-icon layui-icon-add-circle"></i>
        文件导入
    </button>
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-circle"></i>
        筛选导入
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
</script>

<script type="text/html" id="table-type">
    {{#if (d.type == '1') { }}
    <span>后台导入</span>
    {{# }else if(d.type == '2'){ }}
    <span>现场注册</span>
    {{# } }}
</script>

<script type="text/html" id="table-sign-out">
    {{#if (d.sign_out == '1') { }}
    <span>否</span>
    {{# }else if(d.sign_out == '2'){ }}
    <span>是</span>
    {{# } }}
</script>

<script type="text/html" id="table-like">
    {{#if (d.like == '1') { }}
    <span>否</span>
    {{# }else if(d.like == '2'){ }}
    <span>是</span>
    {{# } }}
</script>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let class_id = "{$class_id}";

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '工号',
                    field: 'userid',
                    align: 'center'
                },
                {
                    title: '姓名',
                    field: 'name',
                    align: 'center'
                },
                {
                    title: '手机号',
                    field: 'mobile',
                    align: 'center',
                },
                {
                    field:'division',
                    title: 'Division',
                    align:'center'
                },
                {
                    field:'department_name',
                    title: '部门',
                    align:'center'
                },
                {
                    field:'type',
                    title: '注册类型',
                    align:'center',
                    templet:'#table-type',
                },
                {
                    field:'sign_out',
                    title: '已退出',
                    align:'center',
                    templet:'#table-sign-out',
                },
                {
                    field:'like',
                    title: '我的最爱',
                    align:'center',
                    templet:'#table-like',
                },
                {
                    field:'qiandao',
                    title: '签到',
                    align:'center',
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/Classes/getMembersList?class_id='+class_id,
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 10,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter',{
                title: '导出' //标题
                ,layEvent: 'export' //事件名，用于 toolbar 事件中使用
                ,icon: 'layui-icon-export' //图标类名
            }]
        });

        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 10,
            })
            return false;
        });

        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            } else if (obj.event === 'import') {
                window.import();
            } else if (obj.event === 'export') {
                window.export();
            }
        });



        window.import = function() {
            layer.open({
                type: 2,
                title: '导入',
                shade: 0.1,
                area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                content: '/Classes/import?class_id='+class_id
            });
        }

        window.add = function() {
            layer.open({
                type: 2,
                title: '筛选导入',
                shade: 0.1,
                area: [common.isModile()?'100%':'900px', common.isModile()?'100%':'600px'],
                content: '/Classes/user?class_id='+class_id
            });
        }

        window.batchRemove = function(obj) {

            var checkIds = common.checkField(obj,'id');
            console.log(checkIds)
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除选中的数据吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/Classes/deleteMembers",
                    data:{id:checkIds},
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.status == 0) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                table.reload('user-table');
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }


        // 数据导出
        window.export = function() {
            var field = form.val('reload');
            layer.confirm('确定要导出数据吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                window.open("{:url('exportMembers')}?sign_out="+field.sign_out+"&like="+field.like+"&keys="+field.keys+"&class_id="+class_id, "_blank");
            });
        };

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
